unit frmMainUnit;

{$mode objfpc}{$H+}

{
The MIT License (MIT)

Copyright (c) 2012 Gustavo Carreno <guscarreno@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  ActnList, Menus, StdActns, ExtCtrls, LCLIntf, IniPropStorage, RxVersInfo,
  CEOResourcesUnit, CEOTypesUnit,
  frmNewsUnit, frmTodoListUnit, frmKeyboardShortcutsUnit,
  frmFAQUnit, frmTipsTricksUnit, frmAdventureGuidesUnit, frmBattleCalcUnit,
  frmProductionChainUnit, frmTradeCalcUnit, frmAboutUnit;


type

  { TfrmMain }

  TfrmMain = class(TForm)
    acMain: TActionList;
    actFileExit: TFileExit;
    actFileShowHide: TAction;
    actHelpSite: TAction;
    actHelpAbout: TAction;
    actTabsTradeCalc: TAction;
    actTabsProdChains: TAction;
    actTabsBattleCalc: TAction;
    actTabsAdventureGuides: TAction;
    actTabsTipsTricks: TAction;
    actTabsFAQ: TAction;
    actTabsKeyboardShortcuts: TAction;
    actTabsTodoList: TAction;
    actTabsNews: TAction;
    actSettingsOptions: TAction;
    apMain: TApplicationProperties;
    ipsMain: TIniPropStorage;
    mnuTradeCalc: TMenuItem;
    mnuProdChains: TMenuItem;
    mnuTabsBattleCalc: TMenuItem;
    mnuTabsAdventureGuides: TMenuItem;
    mnuTabsTipsTricks: TMenuItem;
    mnuTabsFAQ: TMenuItem;
    mnuTabsKeyboardShortcuts: TMenuItem;
    mnuTabsTodoList: TMenuItem;
    mnuTabsNews: TMenuItem;
    mnuTabs: TMenuItem;
    mnuSettingsOptions: TMenuItem;
    mnuSettings: TMenuItem;
    mnuHelpSite: TMenuItem;
    mnuHelpAbout: TMenuItem;
    mnuHelp: TMenuItem;
    mnuFileShowHide: TMenuItem;
    mnuSep1: TMenuItem;
    pcMain: TPageControl;
    pmnuSep1: TMenuItem;
    pmnuFileShowHide: TMenuItem;
    pmnuFileExit: TMenuItem;
    mnuFileExit: TMenuItem;
    mnuFile: TMenuItem;
    mmMain: TMainMenu;
    pmTrayIcon: TPopupMenu;
    tsAbout: TTabSheet;
    tsTradeCalculator: TTabSheet;
    tsProductionChains: TTabSheet;
    tsBattleCalculator: TTabSheet;
    tsAdventureGuides: TTabSheet;
    tsTipsAndTricks: TTabSheet;
    tsFAQ: TTabSheet;
    tsKeyboardShortcuts: TTabSheet;
    tsTodoList: TTabSheet;
    tsNews: TTabSheet;
    viMain: TRxVersionInfo;
    stMain: TStatusBar;
    tiMain: TTrayIcon;
    procedure actFileShowHideExecute(Sender: TObject);
    procedure actHelpAboutExecute(Sender: TObject);
    procedure actHelpSiteExecute(Sender: TObject);
    procedure actSettingsOptionsExecute(Sender: TObject);
    procedure actTabsAdventureGuidesExecute(Sender: TObject);
    procedure actTabsBattleCalcExecute(Sender: TObject);
    procedure actTabsFAQExecute(Sender: TObject);
    procedure actTabsKeyboardShortcutsExecute(Sender: TObject);
    procedure actTabsNewsExecute(Sender: TObject);
    procedure actTabsProdChainsExecute(Sender: TObject);
    procedure actTabsTipsTricksExecute(Sender: TObject);
    procedure actTabsTodoListExecute(Sender: TObject);
    procedure actTabsTradeCalcExecute(Sender: TObject);
    procedure apMainHint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure pcMainCloseTabClicked(Sender: TObject);
    procedure tsNewsMouseEnter(Sender: TObject);
    procedure tsNewsMouseLeave(Sender: TObject);
  private
    FVersionMajor, FVersionMinor, FVersionRevision, FVersionBuild: Integer;
    FUpdating: Boolean;
    FfrmNews: TfrmNews;
    FfrmTodoList: TfrmTodoList;
    FfrmKeyboardShortcuts: TfrmKeyboardShortcuts;
    FfrmFAQ: TfrmFAQ;
    FfrmTipsTricks: TfrmTipsTricks;
    FfrmAdventureGuides: TfrmAdventureGuides;
    FfrmBattleCalc: TfrmBattleCalc;
    FfrmProductionChain: TfrmProductionChain;
    FfrmTradeCalc: TfrmTradeCalc;
    FfrmAbout: TfrmAbout;

    procedure CreateTabContent;
    procedure CreateTabs;
    procedure SetTitles;
    procedure SetTitleColors;
    procedure SetVisibleTabs;
  public
    { public declarations }
  end; 

var
  frmMain: TfrmMain;

implementation

uses
  frmSettingsUnit;

{$R *.lfm}

{ TfrmMain }

procedure TfrmMain.FormCreate(Sender: TObject);
begin
//  viMain.
  FVersionMajor    := 0;
  FVersionMinor    := 1;
  FVersionRevision := 10;
  FVersionBuild    := 63;

  FUpdating:= False;

  Caption := Format(rsMainCaption + rsVersionCaption, [FVersionMajor,FVersionMinor,FVersionRevision,FVersionBuild]);
  ipsMain.IniFileName:= envAppConfigFile;

  CreateTabs;
  CreateTabContent;
  SetTitles;
  SetTitleColors;
  pcMain.TabPosition:= TTabPosition(CEOSettings.TabsPosition);
  SetVisibleTabs;
end;

procedure TfrmMain.pcMainCloseTabClicked(Sender: TObject);
begin
  // TODO: Get Number of the tab
  if Sender is TTabSheet then
  begin
    with Sender as TTabSheet do
    begin
      ShowMessage('Closing tab: '+Caption);
      TabVisible:= False;
      CEOSettings.VisibleTabs[PageIndex]:= False;
      CEOSettings.SaveSettings;
    end;
  end;
end;

procedure TfrmMain.tsNewsMouseEnter(Sender: TObject);
begin
  stMain.Panels[1].text:= 'Mouse Enter';
end;

procedure TfrmMain.tsNewsMouseLeave(Sender: TObject);
begin
  stMain.Panels[1].text:= 'Mouse Leave';
end;

procedure TfrmMain.actHelpSiteExecute(Sender: TObject);
begin
  OpenURL(cSiteURL);
end;

procedure TfrmMain.actSettingsOptionsExecute(Sender: TObject);
var
  mResult: Integer;
begin
  frmSettings:= TfrmSettings.Create(Application);
  try
    mResult:= frmSettings.ShowModal;
    if mResult = mrOk then
    begin
      pcMain.TabPosition:=TTabPosition(CEOSettings.TabsPosition);
      SetVisibleTabs;
    end;
  finally
    frmSettings.Free;
  end;
end;

procedure TfrmMain.actTabsAdventureGuidesExecute(Sender: TObject);
begin
  if tsAdventureGuides.TabVisible then
  begin
    pcMain.TabIndex:= cTabAdvGuides;
  end;
end;

procedure TfrmMain.actTabsBattleCalcExecute(Sender: TObject);
begin
  if tsBattleCalculator.TabVisible then
  begin
    pcMain.TabIndex:= cTabBattleCalc;
  end;
end;

procedure TfrmMain.actTabsFAQExecute(Sender: TObject);
begin
  if tsFAQ.TabVisible then
  begin
    pcMain.TabIndex:= cTabFAQ;
  end;
end;

procedure TfrmMain.actTabsKeyboardShortcutsExecute(Sender: TObject);
begin
  if tsKeyboardShortcuts.TabVisible then
  begin
    pcMain.TabIndex:= cTabKeyboardShortcuts;
  end;
end;

procedure TfrmMain.actTabsNewsExecute(Sender: TObject);
begin
  if tsNews.TabVisible then
  begin
    pcMain.TabIndex:= cTabNews;
  end;
end;

procedure TfrmMain.actTabsProdChainsExecute(Sender: TObject);
begin
  if tsProductionChains.TabVisible then
  begin
    pcMain.TabIndex:= cTabProdChains;
  end;
end;

procedure TfrmMain.actTabsTipsTricksExecute(Sender: TObject);
begin
  if tsTipsAndTricks.TabVisible then
  begin
    pcMain.TabIndex:= cTabTipsTricks;
  end;
end;

procedure TfrmMain.actTabsTodoListExecute(Sender: TObject);
begin
  if tsTodoList.TabVisible then
  begin
    pcMain.TabIndex:= cTabTodolist;
  end;
end;

procedure TfrmMain.actTabsTradeCalcExecute(Sender: TObject);
begin
  if tsTradeCalculator.TabVisible then
  begin
    pcMain.TabIndex:= cTabTradeCalc;
  end;
end;

procedure TfrmMain.apMainHint(Sender: TObject);
begin
  stMain.Panels[0].Text:= Application.Hint;
end;

procedure TfrmMain.actHelpAboutExecute(Sender: TObject);
begin
  pcMain.TabIndex:= cTabAbout;
end;

procedure TfrmMain.actFileShowHideExecute(Sender: TObject);
begin
  ShowMessage('Implement Show/Hide !');
end;

procedure TfrmMain.SetTitles;
begin
  FfrmNews.panNewsTitle.Caption:= rsTabNews;

  FfrmTodoList.panTodoListTitle.Caption:= rsTabTodolist;

  FfrmKeyboardShortcuts.panKSTitle.Caption:= rsTabKeyboardShortcuts;

  FfrmFAQ.panFAQTitle.Caption:= rsTabFAQ;

  FfrmTipsTricks.panTTTitle.Caption:= rsTabTipTricks;

  FfrmAdventureGuides.panAdvGuideTitle.Caption:= rsTabAdvGuides;

  FfrmBattleCalc.panBattleCalcTitle.Caption:= rsTabBattleCalc;

  FfrmProductionChain.panProdChainTitle.Caption:= rsTabProdChains;

  FfrmTradeCalc.panTradeCalcTitle.Caption:= rsTabTradeCalc;

  FfrmAbout.panAboutTitle.Caption:= rsTabAbout;
end;

procedure TfrmMain.CreateTabs;
begin
  tsNews.Caption:=              rsTabNews;
  tsTodoList.Caption:=          rsTabTodolist;
  tsKeyboardShortcuts.Caption:= rsTabKeyboardShortcuts;
  tsFAQ.Caption:=               rsTabFAQ;
  tsTipsAndTricks.Caption:=     rsTabTipTricks;
  tsAdventureGuides.Caption:=   rsTabAdvGuides;
  tsBattleCalculator.Caption:=  rsTabBattleCalc;
  tsProductionChains.Caption:=  rsTabProdChains;
  tsTradeCalculator.Caption:=   rsTabTradeCalc;
  tsAbout.Caption:=             rsTabAbout;
end;

procedure TfrmMain.CreateTabContent;
begin
  FfrmNews:= TfrmNews.Create(tsNews);
  FfrmNews.Parent:= tsNews;
  FfrmNews.Initialize;

  FfrmTodoList:= TfrmTodoList.Create(tsTodoList);
  FfrmTodoList.Parent:= tsTodoList;
  FfrmTodoList.Initialize;

  FfrmKeyboardShortcuts:= TfrmKeyboardShortcuts.Create(tsKeyboardShortcuts);
  FfrmKeyboardShortcuts.Parent:= tsKeyboardShortcuts;
  FfrmKeyboardShortcuts.Initialize;

  FfrmFAQ:= TfrmFAQ.Create(tsFAQ);
  FfrmFAQ.Parent:= tsFAQ;
  FfrmFAQ.Initialize;

  FfrmTipsTricks:= TfrmTipsTricks.Create(tsTipsAndTricks);
  FfrmTipsTricks.Parent:= tsTipsAndTricks;
  FfrmTipsTricks.Initialize;

  FfrmAdventureGuides:= TfrmAdventureGuides.Create(tsAdventureGuides);
  FfrmAdventureGuides.Parent:= tsAdventureGuides;
  FfrmAdventureGuides.Initialize;

  FfrmBattleCalc:= TfrmBattleCalc.Create(tsBattleCalculator);
  FfrmBattleCalc.Parent:= tsBattleCalculator;
  FfrmBattleCalc.Initialize;

  FfrmProductionChain:= TfrmProductionChain.Create(tsProductionChains);
  FfrmProductionChain.Parent:= tsProductionChains;
  FfrmProductionChain.Initialize;

  FfrmTradeCalc:= TfrmTradeCalc.Create(tsTradeCalculator);
  FfrmTradeCalc.Parent:= tsTradeCalculator;
  FfrmTradeCalc.Initialize;

  FfrmAbout:= TfrmAbout.Create(tsAbout);
  FfrmAbout.Parent:= tsAbout;
  FfrmAbout.Initialize;
end;

procedure TfrmMain.SetTitleColors;
const
  RedBG = 217;
  GreenBG = 130;
  BlueBG = 96;
  RedFont = 247;
  GreenFont = 246;
  BlueFont = 246;
begin
  FfrmNews.panNewsTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmNews.panNewsTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmTodoList.panTodoListTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmTodoList.panTodoListTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmKeyboardShortcuts.panKSTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmKeyboardShortcuts.panKSTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmFAQ.panFAQTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmFAQ.panFAQTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmTipsTricks.panTTTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmTipsTricks.panTTTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmAdventureGuides.panAdvGuideTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmAdventureGuides.panAdvGuideTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmBattleCalc.panBattleCalcTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmBattleCalc.panBattleCalcTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmProductionChain.panProdChainTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmProductionChain.panProdChainTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmTradeCalc.panTradeCalcTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmTradeCalc.panTradeCalcTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);

  FfrmAbout.panAboutTitle.Color:= RGBToColor(RedBG, GreenBG, BlueBG);
  FfrmAbout.panAboutTitle.Font.Color:= RGBToColor(RedFont, GreenFont, BlueFont);
end;

procedure TfrmMain.SetVisibleTabs;
begin
  tsNews.TabVisible              := CEOSettings.VisibleTabs[cTabNews];
  tsTodoList.TabVisible          := CEOSettings.VisibleTabs[cTabTodolist];
  tsKeyboardShortcuts.TabVisible := CEOSettings.VisibleTabs[cTabKeyboardShortcuts];
  tsFAQ.TabVisible               := CEOSettings.VisibleTabs[cTabFAQ];
  tsTipsAndTricks.TabVisible     := CEOSettings.VisibleTabs[cTabTipsTricks];
  tsAdventureGuides.TabVisible   := CEOSettings.VisibleTabs[cTabAdvGuides];
  tsBattleCalculator.TabVisible  := CEOSettings.VisibleTabs[cTabBattleCalc];
  tsProductionChains.TabVisible  := CEOSettings.VisibleTabs[cTabProdChains];
  tsTradeCalculator.TabVisible   := CEOSettings.VisibleTabs[cTabTradeCalc];

  actTabsNews.Enabled              := CEOSettings.VisibleTabs[cTabNews];
  actTabsTodoList.Enabled          := CEOSettings.VisibleTabs[cTabTodolist];
  actTabsKeyboardShortcuts.Enabled := CEOSettings.VisibleTabs[cTabKeyboardShortcuts];
  actTabsFAQ.Enabled               := CEOSettings.VisibleTabs[cTabFAQ];
  actTabsTipsTricks.Enabled        := CEOSettings.VisibleTabs[cTabTipsTricks];
  actTabsAdventureGuides.Enabled   := CEOSettings.VisibleTabs[cTabAdvGuides];
  actTabsBattleCalc.Enabled        := CEOSettings.VisibleTabs[cTabBattleCalc];
  actTabsProdChains.Enabled        := CEOSettings.VisibleTabs[cTabProdChains];
  actTabsTradeCalc.Enabled         := CEOSettings.VisibleTabs[cTabTradeCalc];

  if CEOSettings.ShowCloseButtons then
  begin
    pcMain.Options:= pcMain.Options + [nboShowCloseButtons];
  end
  else
  begin
    pcMain.Options:= pcMain.Options - [nboShowCloseButtons];
  end;
end;

end.

